package org.osflash.signals;

class PrioritySignal extends Signal, implements IPrioritySignal {

	public function new(valueClasses:Array<Dynamic>=null) {
		super(valueClasses);
	}

	/**
	 * @inheritDoc
	 * @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
	 * @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
	 */
	public function addWithPriority(listener : Dynamic, priority : Int = 0) : ISlot {
		return registerListenerWithPriority(listener, false, priority);
	}

	/**
	 * @inheritDoc
	 * @throws flash.errors.IllegalOperationError <code>IllegalOperationError</code>: You cannot addOnce() then add() the same listener without removing the relationship first.
	 * @throws ArgumentError <code>ArgumentError</code>: Given listener is <code>null</code>.
	 */
	public function addOnceWithPriority(listener : Dynamic, priority : Int = 0) : ISlot {
		return registerListenerWithPriority(listener, true, priority);
	}

	override function registerListener(listener : Dynamic, once : Bool = false) : ISlot {
		return registerListenerWithPriority(listener, once);
	}

	function registerListenerWithPriority(listener : Dynamic, once : Bool = false, priority : Int = 0) : ISlot {
		if(registrationPossible(listener, once))  {
			var slot : ISlot = new Slot(listener, this, once, priority);
			slots = slots.insertWithPriority(slot);
			return slot;
		}
		return slots.find(listener);
	}

}

